package model;

import lombok.Data;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;

/**
 * 集合模型（类似于MySQL中的表）
 * 用于管理一组相关的键值对数据
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Collection implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    /**
     * 集合名称
     */
    private String name;
    
    /**
     * 集合描述
     */
    private String description;
    
    /**
     * 创建时间
     */
    private LocalDateTime createTime;
    
    /**
     * 更新时间
     */
    private LocalDateTime updateTime;
    
    /**
     * 数据存储（使用ConcurrentHashMap保证线程安全）
     */
    private Map<String, Object> data;
    
    /**
     * 集合大小
     */
    private Long size;
    
    /**
     * 是否启用
     */
    private Boolean enabled;
    
    public Collection(String name, String description) {
        this.name = name;
        this.description = description;
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
        this.data = new ConcurrentHashMap<>();
        this.size = 0L;
        this.enabled = true;
    }
    
    /**
     * 添加数据
     */
    public void put(String key, Object value) {
        data.put(key, value);
        this.size = (long) data.size();
        this.updateTime = LocalDateTime.now();
    }
    
    /**
     * 获取数据
     */
    public Object get(String key) {
        return data.get(key);
    }
    
    /**
     * 删除数据
     */
    public Object remove(String key) {
        Object removed = data.remove(key);
        this.size = (long) data.size();
        this.updateTime = LocalDateTime.now();
        return removed;
    }
    
    /**
     * 检查键是否存在
     */
    public boolean containsKey(String key) {
        return data.containsKey(key);
    }
    
    /**
     * 获取所有键
     */
    public java.util.Set<String> keySet() {
        return data.keySet();
    }
    
    /**
     * 获取所有值
     */
    public java.util.Collection<Object> values() {
        return data.values();
    }
    
    /**
     * 清空集合
     */
    public void clear() {
        data.clear();
        this.size = 0L;
        this.updateTime = LocalDateTime.now();
    }
} 